package com.lawrence.order.service;

import com.lawrence.feign.clients.UserClient;
import com.lawrence.feign.pojo.User;
import com.lawrence.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author wanglanglang
 */
@Service
public class OrderService {

    @Autowired
    UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.模拟查询订单
        Order order = new Order(1L, 200L, "耳机", 100, 1L, null);
//         2.用Feign远程调用
        User user = userClient. queryOrderById(order.getUserId());
//         3.封装user到Order
        order.setUser(user);
//         4.返回
        return order;
    }


//    @Autowired
//    private RestTemplate restTemplate;
//
//    /**
//     * 模拟根据 id 查询订单信息
//     * @param orderId
//     * @return order
//     */
//    public Order queryOrderById(Long orderId) {
//        // 1.模拟查询订单
//        Order order = new Order(1L, 200L, "耳机", 100, 1L, null);
//        // 2.利用RestTemplate发起http请求，查询用户
//        // 2.1.url路径
//        String url = "http://userservice/user/" + order.getUserId();
//        // 2.2.发送http请求，实现远程调用
//        User user = restTemplate.getForObject(url, User.class);
//        // 3.封装user到Order
//        order.setUser(user);
//        // 4.返回
//        return order;
//    }
}
